%for grids method and loading data

function Fig2_a_2()
data=829;
Fcolor='k';
Bcolor='w';
Fsize=18;  %font size
result = CS1_0_extraction_2D_Phi(data , 0);

f=figure('name',['Grid:',num2str(data)]);


[nr_sub,nc_sub] = size(result.phi);
tmpy = meshgrid(1:nr_sub)';
tmpx = meshgrid(1:nc_sub);
tmpy = reshape(tmpy,[],1);
tmpx = reshape(tmpx,[],1);
[fitobj,~,~]=fit([tmpy,tmpx],reshape(result.phi,[],1),'poly11');
map_bkg = meshgrid(1:nr_sub)'*fitobj.p10 + meshgrid(1:nc_sub)*fitobj.p01 ;


phi = (result.phi - map_bkg -mean(mean(result.phi))) * 1e3;
win = gausswin(201,100*2.57/2)*gausswin(201,100*2.57/2)'; %std=2nm
phi_plot = conv2(phi,win,'same')/sum(sum(win));
imagesc(result.rx(1,:)-min(result.rx(1,:)),result.ry(:,1)-min(result.ry(:,1)), phi_plot);

ax = gca;
ax.YDir='normal';ax.XDir='normal';
axis equal;axis square;
f.Color = Bcolor;ax.Color=Bcolor;
ax.XColor=Fcolor;ax.YColor=Fcolor;
xl=xlabel('X (nm)');yl=ylabel('Y (nm)');
yl.FontSize=Fsize;xl.FontSize=Fsize;
cMap=getColor('Magma');
colormap(cMap);
ax.CLim=[-2,1];
assignin('base','data',result)
axis([0,abs(result.rx(1,1)-result.rx(1,end)),0,abs(result.ry(end,1)-result.ry(1,1))]);


end